package com.gzwanchuang.xhgj.ui.page.home;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;

import com.gzwanchuang.xhgj.BuildConfig;
import com.gzwanchuang.xhgj.R;
import com.gzwanchuang.xhgj.bridge.data.repository.WebApis;
import com.gzwanchuang.xhgj.bridge.request.HomeRequestViewModel;
import com.gzwanchuang.xhgj.bridge.state.home.JieSuanKaViewModel;
import com.gzwanchuang.xhgj.databinding.FragmentJieSuanKaBinding;
import com.gzwanchuang.xhgj.ui.base.BaseFragment;
import com.gzwanchuang.xhgj.ui.web.ui.WebViewActivity;

/**
 * 文件名 : HomeRootFragment
 * 创建者 : MangoWang
 * 创建日期 : 2019/12/24 16:20
 * 微信 : MangoWaWang
 * 邮箱 : 763482205@qq.com
 * 描述 : TODO
 */
public class JieSuanKaFragment extends BaseFragment {
    private FragmentJieSuanKaBinding mBinding;
    private JieSuanKaViewModel mViewModel;
    private HomeRequestViewModel mRequestViewModel;
    private String TAG = "mango";


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mViewModel = ViewModelProviders.of(this).get(JieSuanKaViewModel.class);
        mRequestViewModel = ViewModelProviders.of(this).get(HomeRequestViewModel.class);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_jie_suan_ka, container, false);
        mBinding = FragmentJieSuanKaBinding.bind(view);
        mBinding.setClick(new ClickProxy());
        mBinding.setVm(mViewModel);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mRequestViewModel.getErrorString().observe(this, s -> {
            hideProgress();
            showShortToast(s);
        });
        mRequestViewModel.getResultString().observe(this, s -> {
            hideProgress();
            showShortToast(s);
        });
        mRequestViewModel.get_bank("1", "2");
        mRequestViewModel.getBank().observe(this, bank -> {
            hideProgress();
            mViewModel.bean.set(bank);
        });

    }

    public class ClickProxy {
        public void back() {
            nav().navigateUp();
        }

        public void change() {
            // 跳转h5
            WebViewActivity.startActivity(mActivity, BuildConfig.SERVERHEAD + WebApis.settlementCard+"?type=2");
        }
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        System.out.println(TAG + "onActivityCreated");
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onStart() {
        // TODO Auto-generated method stub
        System.out.println(TAG + "onStart");
        super.onStart();
    }

    @Override
    public void onResume() {
        // TODO Auto-generated method stub
        System.out.println(TAG + "onResume");
        super.onResume();
    }

    @Override
    public void onPause() {
        // TODO Auto-generated method stub
        System.out.println(TAG + "onPause");
        super.onPause();
    }

    @Override
    public void onStop() {
        // TODO Auto-generated method stub
        System.out.println(TAG + "onStop");
        super.onStop();
    }

    @Override
    public void onDestroyView() {
        // TODO Auto-generated method stub
        System.out.println(TAG + "onDestroyView");
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        System.out.println(TAG + "onDestroy");
        super.onDestroy();
    }

    @Override
    public void onDetach() {
        // TODO Auto-generated method stub
        System.out.println(TAG + "onDetach");
        super.onDetach();
    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            showProgress();
            mRequestViewModel.get_bank("1", "1");
            hideProgress();
        }
    }
}
